

import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.swing.border.*;

import org.json.JSONObject;

import java.awt.*;

public class WelcomePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField TF_address;
	private JTextField TF_port;
	private JLabel L_mainLabel;
	private JLabel L_en_mainLabel;
	private JPanel panel;
	private JLabel L_welcome_server;
	private JLabel L_address;
	private JButton B_change;
	private JButton B_enter;
	private JLabel L_port;
	/**
	 * Create the panel.
	 */
	/*
	public void removeAll(){
		textField.setVisible(false);
		textField_1.setVisible(false);
		panel.setVisible(false);
		label.setVisible(false);
		lblNewLabel.setVisible(false);
		lblNewLabel_1.setVisible(false);
		lblNewLabel_2.setVisible(false);
		
	}
	*/
	private boolean checkPort(String s_port) {
		try{
			int I_port = Integer.parseInt(s_port); 
			if(I_port>=10000 && I_port < 65535)	{
				return true;
			}else{
				JOptionPane.showMessageDialog(null, "端口需为一个在(10000,65535)之间的整数","检查端口",JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}catch(NumberFormatException e){
			JOptionPane.showMessageDialog(null, "端口需为整数","检查端口",JOptionPane.ERROR_MESSAGE);
			
		}catch(Exception e){
			JOptionPane.showMessageDialog(null, "未知错误","检查端口",JOptionPane.ERROR_MESSAGE);
		}
		return false;
		
	}
	private boolean checkNet(String s_address,String s_port){
		try{
			JSONObject obj = new JSONObject(new SocketOp(s_address,s_port).request("{\"method\":\"SayHi\"}"));
			if(obj.getString("method").equals("SayHi")){
				return true;
			}
		}catch(Exception e){
			return false;
		}
		return false;
	}
	public WelcomePanel() {
		setBorder(BorderFactory.createTitledBorder("welcome"));
		L_mainLabel = new JLabel("欢迎使用宿舍管理系统");
		L_mainLabel.setBounds(80, 29, 224, 48);
		L_mainLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		
		L_en_mainLabel = new JLabel("Welcome to Dormitory Control System");
		L_en_mainLabel.setBounds(192, 102, 252, 14);
		L_en_mainLabel.setFont(new Font("等线", Font.ITALIC, 13));
		
		panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.inactiveCaption));
		panel.setBounds(58, 139, 384, 256);
		
		L_welcome_server = new JLabel("服务器配置：");
		L_welcome_server.setBounds(10, 10, 128, 33);
		L_welcome_server.setFont(new Font("黑体", Font.ITALIC, 20));
		
		L_address = new JLabel("Address:");
		L_address.setHorizontalAlignment(SwingConstants.RIGHT);
		L_address.setBounds(90, 66, 69, 15);
		
		TF_address = new JTextField();
		TF_address.setBounds(187, 61, 120, 24);
		TF_address.setEditable(false);
		TF_address.setText(new SocketOp().getIp());
		TF_address.setFont(new Font("Arial", Font.PLAIN, 15));
		TF_address.setColumns(10);
		
		L_port = new JLabel("Port:");
		L_port.setHorizontalAlignment(SwingConstants.RIGHT);
		L_port.setBounds(98, 108, 61, 15);
		
		TF_port = new JTextField();
		TF_port.setBounds(187, 103, 120, 24);
		TF_port.setEditable(false);
		TF_port.setText(Integer.toString(new SocketOp().getPort()));
		TF_port.setFont(new Font("Arial", Font.PLAIN, 15));
		TF_port.setColumns(10);
		
		B_change = new JButton("修改");
		B_change.setBounds(111, 164, 78, 23);
		

		B_enter = new JButton("确认");
		B_enter.setBounds(199, 164, 78, 23);
		B_enter.setEnabled(false);
		B_enter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String sendMsg = "{\n	\"remote_ip\":\""+TF_address.getText()+"\",\n	\"remote_port\":"+TF_port.getText()+"\n}";
				if(checkPort(TF_port.getText())){
					if(checkNet(TF_address.getText(),TF_port.getText())){
						try{
							BufferedWriter out = new BufferedWriter(new FileWriter("config.json"));
							out.write(sendMsg);
							out.close();
							B_change.setEnabled(true);
							B_enter.setEnabled(false);
							
							
						}catch(Exception e1){
							JOptionPane.showMessageDialog(null, "系统错误，错误代码：1010");
						}
						TF_address.setEditable(false);
						TF_port.setEditable(false);
					}else{
						if(JOptionPane.showConfirmDialog(null, "未检测到服务器运行，确认使用此服务器端口？", "", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION)
						{
							try{
								BufferedWriter out = new BufferedWriter(new FileWriter("config.json"));
								out.write(sendMsg);
								out.close();
								B_change.setEnabled(true);
								B_enter.setEnabled(false);
								
								
							}catch(Exception e1){
								JOptionPane.showMessageDialog(null, "系统错误，错误代码：1010");
							}
							TF_address.setEditable(false);
							TF_port.setEditable(false);
						}else{
							TF_address.setText(new SocketOp().getIp());
							TF_port.setText(Integer.toString(new SocketOp().getPort()));
							TF_address.setEditable(false);
							TF_port.setEditable(false);
						}
					}
					
				}
				
			}
		});
		B_change.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TF_address.setEditable(true);
				TF_port.setEditable(true);
				B_change.setEnabled(false);
				B_enter.setEnabled(true);
			}
		});
		setLayout(null);
		add(L_mainLabel);
		add(panel);
		panel.setLayout(null);
		panel.add(L_welcome_server);
		panel.add(L_address);
		panel.add(L_port);
		panel.add(B_change);
		panel.add(B_enter);
		panel.add(TF_address);
		panel.add(TF_port);
		add(L_en_mainLabel);

	}

}
